'''
Created on Mar 20, 2013

@author: jason.richmond
This module is used to create the user model for our pricing simulation.

The users generate jobs for a series of applications valid applications include
E-mail
Voice
Telnet
FTP (File transfer)

In general it is assumed users have the following preferences:
Email (No flags set)
Telnet (Both flags set)
Voice  (Priority set, No drop not set)
FTP (Priority not set, No drop set)

In our simulations we will simulate how price may effect these preferences.

'''

from SimPy.Simulation import *
from Globals import *
from Server import Server
from Packet import *

class UserModel(Process):
    #The authors created FTP jobs over 50% of the time
    #E-mail and voice were balanced with remote login an extremely small percentage
    #remote login was usually 1% or less of all traffic.
    CreationRate = None
    
    def __init__(self):
        Process.__init__(self)
        
    def Run(self):
        while 1:
            #wait to create next job
            yield hold,self,G.Rnd.expovariate(UserModel.CreationRate)
            
            #create next job and add to job queue
            self.createJob()
            
            #Activate next idle machine
            reactivate(Server)
    
    def createJob(self):
        packetType = G.Rnd.random()
        
        if packetType < G.TelnetRate:
            J = TelnetPacket()
        elif packetType < (G.TelnetRate + G.EmailRate):
            J = EmailPacket()
        elif packetType < (G.TelnetRate + G.EmailRate + G.VoiceRate):
            J = VoicePacket()
        else:
            J = FTPPacket()
            
        if J.priorityServiceFlag:
            Server.PriorityQueue.append(J)
        else:
            Server.Queue.append(J)        